﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.DataStore;

namespace NTTDATA.BEENChina.ResultCertification.UI
{    
    public partial class FormImportTestData : Form
    {        
        public FormImportTestData()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                List<string[]> datas = resumeStoreData(this.txtData.Text);
                saveToDatabase(this.txtVertifyId.Text, datas);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message +"\r\n 异常终了");
            }
            MessageBox.Show("OK");
        }

        private List<string[]> resumeStoreData(string testData)
        {
            List<string[]> testDatas = new List<string[]>();
            string[] lines = testData.Split(new char[] { '\n' });
            foreach (string line in lines)
            {
                if (line.Trim() == string.Empty)
                {
                    continue;
                }
                string[] data = splitRow(line);
                if(data!=null)
                {
                    testDatas.Add(data); ;
                }
            }
            return testDatas;
        }

        private string[] splitRow(string line)
        {
            string[] data = new string[4];
            try
            {             
                string[] columns = line.Split(new char[] { '\t' });

                data[0] = columns[0];  //categoryId
                data[1] = columns[3];  //EntryResult
                data[2] = columns[4];  //CommentId
                //data[3].FormId = columns[];

                return data;
            }
            catch (Exception exp)
            {
                if (MessageBox.Show(exp.Message + ",\r\n是否继续", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    throw exp;
                }
                return null;
            }
        }

        private void saveToDatabase(string vertifyId, List<string[]> storeDatas)
        {            
            TestDataAccess access = new TestDataAccess();
            access.InsertTestData(vertifyId, storeDatas);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }


}
